package com.project.datacenter.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.datacenter.dto.ParamDto;
import com.project.datacenter.entity.Enterprise;
import com.project.datacenter.entity.TobjEnterprise;
import com.project.datacenter.mapper.EnterpriseMapper;
import com.project.datacenter.mapper.TobjEnterpriseMapper;
import com.project.datacenter.service.EnterpriseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EnterpriseServiceImpl implements EnterpriseService {

    @Resource
    private EnterpriseMapper enterpriseMapper;
    @Resource
    private TobjEnterpriseMapper tobjEnterpriseMapper;

    @Override
    @DS("datacenter")
    public List<TobjEnterprise> getList() {
        return tobjEnterpriseMapper.getList();
    }

    @Override
    @DS("master")
    public List<TobjEnterprise> getListByDate(ParamDto paramDto) {
        return tobjEnterpriseMapper.selectByDate(paramDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void saveList(List<TobjEnterprise> list) {
        for (TobjEnterprise entity : list) {
            tobjEnterpriseMapper.insertSelective(entity);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteByDate(ParamDto paramDto) {
        tobjEnterpriseMapper.deleteByDate(paramDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteExpireDate(ParamDto paramDto) {
        tobjEnterpriseMapper.deleteExpireDate(paramDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteAll() {
        enterpriseMapper.deleteAll();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void insert(List<TobjEnterprise> list) {
        for (TobjEnterprise tobjEnterprise : list) {
            Enterprise enterprise = new Enterprise();
            enterprise.setId(tobjEnterprise.getEnterprise_id());
            enterprise.setYhmc(tobjEnterprise.getCnname());
            if (!StringUtils.isEmpty(tobjEnterprise.getAbbreviation())) {
                enterprise.setYhjc(tobjEnterprise.getAbbreviation());
            } else {
                enterprise.setYhjc(tobjEnterprise.getEnname());
            }
            enterprise.setJyxkzh(tobjEnterprise.getBusinesslicense());
            enterprise.setBazh(tobjEnterprise.getOrganizationcode());
            enterprise.setFrdb(tobjEnterprise.getLegalperson());
            enterprise.setLxdh(tobjEnterprise.getPhonenumber());
            enterprise.setDzyj(tobjEnterprise.getEmail());
            enterprise.setWzdz(tobjEnterprise.getWebsite());
            enterprise.setGjqybh(tobjEnterprise.getBusbasenumber());
            enterprise.setPwh(tobjEnterprise.getBasenumber());
            enterpriseMapper.insertSelective(enterprise);
        }
    }
}
